window.HTMLElement = window.HTMLElement || Element;
window.HTMLElement.prototype.lPaging = function(options, messages) {
	function creatPaingDiv(_this, _op, _messages) {
		var obj = new Object();
		var strHtml = '';

		obj = document.createElement("div");

		obj.className = 'l-paging-div';
		strHtml = '<a class="l-paging-pgarrow" id="'+_op.btnFirst+'"><i class="l-icon l-icon-paging-first"></i></a>						  '
		strHtml += '	<a class="l-paging-pgarrow l-paging-prev" id="'+_op.btnPrev+'"><i class="l-icon l-icon-paging-prev"></i></a>		  '
		strHtml += '	<span class="l-paging-pgnum l-font-size13">													  '
		strHtml += '		<span class="l-paging-this">' + _op.thisPage + '</span>/' + _op.total + '													  '
		strHtml += '	</span>																						  '
		strHtml += '	<a class="l-paging-pgarrow l-paging-next" id="'+_op.btnNext+'"><i class="l-icon l-icon-paging-next"></i></a>		  '
		strHtml += '	<a class="l-paging-pgarrow" id="'+_op.btnLast+'"><i class="l-icon l-icon-paging-last"></i></a>					  '
		strHtml += '	<span class="l-paging-span">																  '
		strHtml += '		<input id="' + _op.inputId + '" type="text" class="l-paging-input">												  '
		strHtml += '		<button type="button" class="l-btn-gray l-paging-btn" id="'+_op.btnInputId+'">'+_messages.lblInputBtn+'</button>						  '
		strHtml += '	</span>																						  '
		strHtml += '	<span class="l-paging-span l-pagin-span-right">												  '
		strHtml += '		<select id="' + _op.selectId + '" class="l-select">																  '
		strHtml += '		</select>																				  '
		strHtml += '		<button type="button" class="l-btn-gray l-paging-btn" id="'+_op.btnSelectId+'">'+_messages.lblSelectBtn+'</button>						  '
		strHtml += '	</span>';

		obj.innerHTML = strHtml;

		document.getElementById(_this.id).appendChild(obj);

		for (var key in _op['selectOption']) {
			var varItem = new Option(key, _op['selectOption'][key]);

			document.getElementById(_op.selectId).options.add(varItem);
		}

		document.getElementById(_op.selectId).value = _op['selectPage'];

		document.getElementById(_op.btnInputId).onclick = function() {
			var thisUrl = window.document.location.href;
			var pageMove = document.getElementById(_op.inputId);
			var pattern = '';
			var resultUrl = '';

			// 양쪽 공백 없애기
			pattern = /(^\s+)|(\s+$)/g;

			pageMoveValue = pageMove.value.replace(pattern, '');

			// 필수 입력 검사
			if (pageMoveValue == '') {
				alert(_messages.checkValidation);
				pageMove.focus();

				return false;
			}

			// 숫자 검사
			pattern = /(^[0-9]+$)/;
			if (!pattern.test(pageMoveValue)) {
				alert(_messages.checkNumber);
				pageMove.focus();

				return false;
			}

			// 범위 검사
			if (parseInt(pageMoveValue) > parseInt(_op.total)) {
				alert(_messages.checkLimit);
				pageMove.focus();

				return false;
			}

			pattern = /intPage=[0-9]*/;
			if (pattern.test(thisUrl)) {
				resultUrl = thisUrl.replace(pattern, 'intPage=' + pageMoveValue);
			} else {
				resultUrl = thisUrl + (new RegExp("\\?$", "g").test(thisUrl) ? '' : (thisUrl.indexOf('?') > -1 ? '&' : '?')) + 'intPage=' + pageMoveValue;
			}

			location.replace(resultUrl);
		};
		document.getElementById(_op.btnSelectId).onclick = function() {
			var thisUrl = window.document.location.href;
			var thisVal = document.getElementById(_op.selectId).value;
			var pattern = '';
			var resultUrl = '';

			pattern = /intPage=[0-9]*/;
			if (pattern.test(thisUrl)) {
				thisUrl = thisUrl.replace(pattern, 'intPage=1');
			} else {
				thisUrl += (new RegExp("\\?$", "g").test(thisUrl) ? '' : (thisUrl.indexOf('?') > -1 ? '&' : '?')) + 'intPage=1';
			}

			pattern = /intPageSize=[0-9]*/;
			if (pattern.test(thisUrl)) {
				resultUrl = thisUrl.replace(pattern, 'intPageSize=' + thisVal);
			} else {
				resultUrl = thisUrl + (new RegExp("\\?$", "g").test(thisUrl) ? '' : (thisUrl.indexOf('?') > -1 ? '&' : '?')) + 'intPageSize=' + thisVal;
			}

			location.replace(resultUrl);
		};

		document.getElementById(_op.btnFirst).onclick = function() {_clickPageBtn(_op.first);}
		document.getElementById(_op.btnPrev).onclick = function() {_clickPageBtn(_op.prev);}
		document.getElementById(_op.btnNext).onclick = function() {_clickPageBtn(_op.next);}
		document.getElementById(_op.btnLast).onclick = function() {_clickPageBtn(_op.total);}
	}

	function _clickPageBtn(val) {
		var thisUrl = window.document.location.href;
		var thisVal = val;
		var pattern = /intPage=[0-9]*/;
		var resultUrl = '';

		if (pattern.test(thisUrl)) {
			resultUrl = thisUrl.replace(pattern, 'intPage=' + thisVal);
		} else {
			resultUrl = thisUrl + (new RegExp("\\?$", "g").test(thisUrl) ? '' : (thisUrl.indexOf('?') > -1 ? '&' : '?')) + 'intPage=' + thisVal;
		}

		location.replace(resultUrl);
	}

	function creatOption(options) {
		var _dops = {
			"thisPage": 1,
			"total": 1,
			"selectPage": 20,
			"selectOption": {
				"10": 10,
				"20": 20,
				"30": 30
			}
		}

		var _op = _dops;

		for (var key in options) {
			_op[key] = options[key];

			if (key == 'selectPage') {
				switch (options[key]) {
					case 10:
						_op[key] = 10;
						break;
					case 20:
						_op[key] = 20;
						break;
					case 30:
						_op[key] = 30;
						break;
					default:
						_op[key] = 20;
						break;
				}
			}
		}

		_op.first = 1;
		_op.prev = 1;
		_op.next = 1;

		if (_op['thisPage'] > 1) {
			_op.prev = _op['thisPage'] - 1;
		}
		if (_op['total'] > 1 && _op['thisPage'] < _op['total']) {
			_op.next = _op['thisPage'] + 1;
		}

		_op.inputId = 'l-page-input-' + Math.round(Math.random() * 10000);
		_op.btnInputId = 'l-page-btn-input-' + Math.round(Math.random() * 10000);
		_op.selectId = 'l-page-select-' + Math.round(Math.random() * 10000);
		_op.btnSelectId = 'l-page-btn-select-' + Math.round(Math.random() * 10000);

		_op.btnFirst = 'l-page-btn-first-' + Math.round(Math.random() * 10000);
		_op.btnPrev = 'l-page-btn-prev-' + Math.round(Math.random() * 10000);
		_op.btnNext = 'l-page-btn-next-' + Math.round(Math.random() * 10000);
		_op.btnLast = 'l-page-btn-last-' + Math.round(Math.random() * 10000);

		return _op;
	}

	function creatMessage(messages) {
		var _dmsgs = {
			"lblInputBtn": '이동',
			"lblSelectBtn": '보기',
			"checkValidation": '이동할 페이지를 입력하여 주십시오.',
			"checkNumber": '이동할 페이지가 숫자가 아닙니다.',
			"checkLimit": '이동할 페이지 법위를 초과하였습니다.'
		}

		var _messages = _dmsgs;

		return _messages;
	}

	if (this.id === '') {
		this.id = 'l-page-' + Math.round(Math.random() * 10000);
	}

	var _op = creatOption(options);
	var _messages = creatMessage(messages);

	creatPaingDiv(this, _op, _messages);
}